<?php
namespace Admin\Model;
use Common\Model;
use Common\Verify;
if(!defined('SITE')) exit('Access Denied');
class ManageModel extends Model {
	const CLERA=1;
	const INCREMENT=2;
	public function __construct(){
		$this->tableName='manage';
		/*
		数据是否可以不传	null
		数据类型	type	
		数值的区间	between
		数值不得在某个区间	notbetween
		数据是否属于某个范围	in
		数据不得属于某个范围	notin
		数据长度的验证	length
		数据是否可以重复	unique
		数据是否符合我们所描述的正则表达式	regex
		数据是否符合某个自定义函数	function
		数据是否符合某个方法	method
		数据是否等于某个值	equal
		数据不得等于某个值	notequal
		两条数据是否一致	confirm
		*/
		$this->validate=array(
			'id'=>array(
				'between'=>'0,4294967295',
				'unique'=>true
			),
			'name'=>array(
				'length'=>'1,32',
				'unique'=>true
			),
			'pname'=>array(
				'length'=>'1,32',
				'unique'=>true
			),
			'power'=>array(
				'between'=>'0,4294967295'
			)
		);
		$this->vTime=array(
			'name'=>array(
				'null'=>array(self::INSERT)
			),
			'pname'=>array(
				'null'=>array(self::INSERT)
			),
			'password'=>array(
				'null'=>array(self::INSERT)
			),
			'rtime'=>array(
				'null'=>array(self::INSERT)
			)
		);
		$this->vMessage=array(
			
		);
		$this->vFieldsAlias=array(
			'name'=>'用户名',
			'pname'=>'笔名'
		);
		parent::__construct();
	}
	public function add($data,$autoValidation=true){
		$data['power']=$this->parsePower(post('power'));
		$data['password']=md5(sha1(post('password')));
		$data['rtime']=time();
		return parent::add($data,$autoValidation);
	}
	public function selectByPage($limit){
		$param=array(
			'sql'=>"SELECT id,name,pname,power,rtime FROM {$this->getTableName()} {$limit}"
		);
		return $this->db->execute($param);
	}
	public function update($data,$pk,$autoValidation=true){
		if(isset($data['name'])){
			unset($data['name']);
		}
		if(isset($data['pname'])){
			unset($data['pname']);
		}
		if(post('password')===''){
			if(isset($data['password'])){
				unset($data['password']);
			}
		}else{
			$data['password']=md5(sha1(post('password')));
		}
		if(isset($data['rtime'])){
			unset($data['rtime']);
		}
		$data['power']=$this->parsePower(post('power'));
		return parent::update($data,$pk,$autoValidation);
	}
	private function parsePower($pmarkArr){
		$power=0;
		if(isset($pmarkArr) && is_array($pmarkArr)){
			foreach ($pmarkArr as $val){
				$power|=$val;
			}
		}
		return $power;
	}
	public function getVCount(){
		$param=array(
			'sql'=>"SELECT `vcount` FROM `{$this->getTableName()}` WHERE name=?",
			'bind'=>array('s',array(post('name')))
		);
		$data=$this->db->execute($param);
		if(isset($data[0]['vcount'])){
			return $data[0]['vcount'];
		}else{
			return 0;
		}
	}
	private function setVCount($flag){
		switch ($flag){
			case self::CLERA:
				$val='0';
				break;
			case self::INCREMENT:
				$val='vcount+1';
				break;
		}
		$param=array(
			'sql'=>"UPDATE `{$this->getTableName()}` SET vcount={$val} WHERE `name`=?",
			'bind'=>array('s',array(post('name')))
		);
		$this->db->execute($param);
	}
	public function loginValidation(){
		if($this->getVCount()>=COUNT_VERIFY){
			$verify=new Verify();
			if(mb_strlen(post('verify'))!=4 || !$verify->check(post('verify'))){
				//验证码错误
				$this->error='请输入正确的验证码.';
				return false;
			}
		}
		$param=array(
			'sql'=>"SELECT `name`,`pname`,`power` FROM `{$this->getTableName()}` WHERE name=? AND password=?",
			'bind'=>array('ss',array(post('name'),md5(sha1(post('password')))))
		);
		$data=$this->db->execute($param);
		if(count($data)!=1){
			//密码输错次数加1
			$this->setVCount(self::INCREMENT);
			$this->error='用户名或密码输入错误.';
			return false;
		}
		//验证成功，vcount清零，将必要的数据存到session中方便在后台以及其他地方使用
		$this->setVCount(self::CLERA);
		$_SESSION=array_merge($_SESSION,$data[0]);
		return true;
	}
}